package cn.kgc.chlm.servlet;

import cn.kgc.chlm.entity.Menu;
import cn.kgc.chlm.entity.Order;
import cn.kgc.chlm.entity.User;
import cn.kgc.chlm.service.MenuService;
import cn.kgc.chlm.service.OrderService;
import cn.kgc.chlm.service.UserService;
import cn.kgc.chlm.service.impl.MenuServiceImpl;
import cn.kgc.chlm.service.impl.OrderServiceImpl;
import cn.kgc.chlm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/order/*")
public class OrderServlet extends BaseServlet{

    public void diancai(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuService menuService = new MenuServiceImpl();

        String menuId = req.getParameter("menu_id");
        //把接受到的参数类型转换int类型
        int mId = Integer.parseInt(menuId);

//        System.out.println("菜品id"+menu_id);
        //获取HttpSession对象
//        HttpSession session = req.getSession();
//        //获取登录的用户对象
//        User loginUser = (User) session.getAttribute("user");
//        if(null == loginUser){
//            req.setAttribute("msg","您需要先登陆后才能访问");
//            req.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(req,resp);
//        }else {
//            //现在我们默认这个对象不可能为空
            Menu menu = menuService.selectMenuById(mId);
            req.setAttribute("menu",menu);
            req.getRequestDispatcher("/WEB-INF/pages/diancai.jsp").forward(req,resp);
//        }

    }

    public void do_diancai(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException {
        //设置我们HttpRequest对象的字符编码格式
        req.setCharacterEncoding("utf-8");
        //获取UserService对象
        OrderService orderService = new OrderServiceImpl();

        UserService userService = new UserServiceImpl();
        //获取HttpSession对象
        HttpSession session = req.getSession();

        //获取表单信息
        String userId = req.getParameter("user_id");
        //将接受到的参数转为int类型
        int uId = Integer.parseInt(userId);
        String menuId = req.getParameter("menu_id");
        int mId = Integer.parseInt(menuId);
        String price = req.getParameter("price");
        double p = Double.parseDouble(price);
        String num = req.getParameter("num");
        int n = Integer.parseInt(num);
        String address = req.getParameter("address");

        //并封装到Order对象
        Order order = new Order();
        order.setUserId(uId);
        order.setMenuId(mId);
        order.setAddress(address);
        order.setNum(n);
        order.setPrice(p);
        order.setCountPrice(p*n);

        //拿到我们登录用户的对象
        User loginUser = (User) session.getAttribute("user");


        int i = orderService.addOrder(order, loginUser);

        resp.sendRedirect(req.getContextPath()+"/order/order_view");
    }

    public void order_view(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        OrderService orderService = new OrderServiceImpl();
        //获取HttpSession对象
        HttpSession session = req.getSession();
        //获取当前登录的用户
        User user = (User) session.getAttribute("user");
//        System.out.println(user);
        List<Order> orders = orderService.selectOrderByUserId(user);
        System.out.println(orders);

        req.setAttribute("order",orders);
        req.getRequestDispatcher("/WEB-INF/pages/order_view.jsp").forward(req,resp);

    }

    public void update_order(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = new OrderServiceImpl();
        MenuService menuService = new MenuServiceImpl();
        List<Menu> menus = menuService.selectMenuALl();
        String orderId = req.getParameter("order_id");
        //将接受到的参数类型强转为int类型
        int oId = Integer.parseInt(orderId);
        Order order = orderService.selectOrderById(oId);
        req.setAttribute("menus",menus);
        req.setAttribute("o",order);
        req.getRequestDispatcher("/WEB-INF/pages/update_order.jsp").forward(req,resp);
    }

    public void del_order(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        String orderId = req.getParameter("order_id");
        int oId = Integer.parseInt(orderId);

        OrderService orderService = new OrderServiceImpl();
        orderService.delOrder(oId);
        resp.sendRedirect(req.getContextPath()+"/order/order_view");
    }
}
